<?php
require_once("lib/user.php");
require_once("lib/manageuserrole.php");

include("header.php");

?>
<?php
	$pageTitle ="Assign role to user";
	$divId = "userroledetails";
	$handlerPage = "lib/manageuserrolehandler.php";
	
	
	
	if(session_id() == '') {
		session_start();
	}
	$key = "user";
	$user = unserialize ( $_SESSION [$key] );
	$manageuserrole =new ManageUserRole();
	$userrolemappings=$manageuserrole->userRoleListAll();
	
?>
<script type="text/javascript">
function updateDetails(){

	var handlerPage="<?PHP echo $handlerPage; ?>";
	var divId="<?PHP echo $divId; ?>";
	
	//Delete all rows in table for refresh
	$("#"+divId+" > div > div> table > tbody > tr").each(function(index){
		$(this).remove();
		
	});

	$.getJSON(handlerPage+"?fulldetails=yes",function(data,status){
		$.each(data, function(counter, userrolemapping) {
			var userId=userrolemapping.uid;
			var userName=userrolemapping.username;
			var roleName=userrolemapping.rolename;

			var bodyelement=$("#"+divId+" > div > div> table > tbody");
			var updateStr= '<a id="userid_'+userId+ '" href="#" onclick="javascript:updateDialog(\'Assign role to User\',\''+handlerPage+'\',\''+handlerPage+'\',\''+userId+'\',\'updateDetails()\')"><span>Edit User</span><div class="modify"></div></a>';
			var deleteStr= '<a id="userid_'+userId+ '" href="#" onclick="javascript:deleteDialog(\'Remove role from User\',\''+handlerPage+'\',\''+handlerPage+'\',\''+userId+'\',\'updateDetails()\')"><span>Delete User</span><div class="delete"></div></a>';
			var rowString='<tr><td>'+(counter+1)+'</td><td>'+userName+'</td><td>'+roleName+'</td><td>'+updateStr+'</td><td>'+deleteStr+'</td></tr>';
			$(rowString).appendTo(bodyelement);
			
			var pane = $('#'+divId+'');
			var api = pane.data('jsp');
			api.reinitialise();
			
		});
	});
	
}
</script>
<div class="maincontent">
	<!------- Header ---------->
	<div class="borderedDiv">
		<table style="width: 100%">
			<tr>
				<td class="titleText blueheadertext">User to Role Mapping</td>
			</tr>
		</table>
	</div>


	<!------- Content ---------->

	<div class="scroll-pane displaytable" id="<?PHP echo $divId;?>">
		<table class="contenttable">
			<thead>
				<tr>
					<td style="width: 8%">Sl. No.</td>
					<td>User</td>
					<td>Role</td>
					<td style="width: 20px">&nbsp;</td>
					<td style="width: 20px">&nbsp;</td>
				</tr>
			</thead>

			<tbody>
<?php
	$i = 1;
	foreach ( $userrolemappings as $userrolemapping ) {
		$userId = $userrolemapping->getUserId ();
		$userName = $userrolemapping->getUserName ();
		$userRole = $userrolemapping->getRoleName ();
		
		echo ("<tr>");
		echo ("<td>" . $i . "</td>");
		echo ("<td>" . $userName . "</td>");
		echo ("<td>" . $userRole . "</td>");
		echo "<td><a id=\"userid_" . $userId . "\" alt=\"Edit\" href=\"#\" onClick=\"javascript:updateDialog('Assign role to User', '" . $handlerPage . "','" . $handlerPage . "'," . $userId . ",'updateDetails()')\"><span>Assign role to User</span><div class=\"apply\"></div></a></td>";
		echo "<td><a id=\"userid_" . $userId . "\" href=\"#\"  onClick=\"javascript:deleteDialog('Remove role from User', '" . $handlerPage . "','" . $handlerPage . "'," . $userId . ",'updateDetails()')\"><span>Remove role from User</span><div class=\"delete\"></div></a></td>";
		echo ("</tr>");
		$i ++;
	}
?>
	</tbody>
		</table>
	</div>
</div>


<?php
	include("footer.php");
?>